package com.hanbang.taitaigou.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.model.GetUserAddressList;
import com.hanbang.taitaigou.ui.activity.aboutme.address.AboutMeAddAddressManagementActivity;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.http.JsonHelper;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Fangzh on 2016/4/1.
 */
public class AddAddressActivity extends BaseActivity {
    //添加地址的textview
    private TextView add_address_tv;
    //地址的listview
    private ListView lv_home_choAddress;
    //地址列表数据
    public static  ArrayList<GetUserAddressList> addressList = new ArrayList();

    private ListAdapter myAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_choose_address);
        initView();
    }

    private void initView() {
        setTop(null, 0, getString(R.string.botton_tab5), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));
        add_address_tv = (TextView) findViewById(R.id.add_address_tv);
        lv_home_choAddress = (ListView) findViewById(R.id.lv_home_choAddress);
        //获取地址列表
        getAddress();
        lv_home_choAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                addressList.get(position).getId();
                getDefaltAddress(addressList.get(position).getId());
                Intent intent = new Intent();
                intent.putExtra("address", addressList.get(position).getAddress());
                intent.putExtra("addressID",addressList.get(position).getId());
                LogUtils.i("tttt", "address是：" + addressList.get(position).getAddress());
                setResult(1001, intent);
                finish();

            }
        });
        add_address_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddAddressActivity.this, AboutMeAddAddressManagementActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                intent.putExtra("type", 1);
                intent.putExtra("addType",2);
                startActivityForResult(intent, 888);
            }
        });


    }

    private void getAddress() {
        HttpRequestParams httpRequestParams = new HttpRequestParams("GetUserAddressList");
        LogUtils.i("yyyyy", "传进来的User_id是：" + userData.getUser_id());
        httpRequestParams.addBodyParameter("User_id", userData.getUser_id() + "");
//        httpRequestParams.addBodyParameter("User_id",92+"");
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在加载...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    addressList.clear();
                    JSONObject json = new JSONObject(result);
                    JSONArray array = json.getJSONArray("result");
                    addressList.addAll(JsonHelper.parseCollection(array, ArrayList.class, GetUserAddressList.class));
                    LogUtils.i("xxxxxx", "addressList的大小是：" + addressList.get(0).toString());
//                    for (int i = 0; i < array.length(); i++) {
//                        id = addressList.get(i).getId();
//                    }
                    myAdapter = new ListAdapter();
                    lv_home_choAddress.setAdapter(myAdapter);
                    myAdapter.notifyDataSetChanged();
                } catch (JSONException e) {

                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /*地址列表Adapter*/
    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return addressList.size();
        }

        @Override
        public Object getItem(int position) {
            return addressList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 常见的优化ViewHolder
            ViewHolder viewHolder;
            GetUserAddressList gal = addressList.get(position);
            LogUtils.i("ttttt", "addressList的大小是：" + addressList.get(0).getPurchname());
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = UiUtils.getInflaterView(AddAddressActivity.this, R.layout.add_address_list);
                viewHolder.layout_addr = (LinearLayout) convertView.findViewById(R.id.layout_addr);
                viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
                viewHolder.phone_tv = (TextView) convertView.findViewById(R.id.phone_tv);
                viewHolder.address_tv = (TextView) convertView.findViewById(R.id.address_tv);
                viewHolder.detail_address_tv = (TextView) convertView.findViewById(R.id.detail_address_tv);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.name_tv.setText(gal.getPurchname());
            viewHolder.phone_tv.setText(gal.getPurchtel());
            viewHolder.address_tv.setText("地址：");
            viewHolder.detail_address_tv.setText(gal.getCity() + gal.getArea() + gal.getAddress());
            return convertView;
        }

        class ViewHolder {
            LinearLayout layout_addr;
            TextView name_tv;
            TextView phone_tv;
            TextView address_tv;
            TextView detail_address_tv;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 888 && resultCode == 1001) {
            getAddress();
        }
    }


    /*设置默认地址的请求*/
    private void getDefaltAddress(int id) {
        HttpRequestParams httpRequestParams = new HttpRequestParams("SetDefaultAddress");
        LogUtils.i("yyyyy", "传进来的User_id是：" + userData.getUser_id());
        httpRequestParams.addBodyParameter("User_id", userData.getUser_id() + "");
        httpRequestParams.addBodyParameter("id", id + "");
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在加载...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
//                    addressList.clear();
                    JSONObject json = new JSONObject(result);
                    JSONArray array = json.getJSONArray("result");
//                    addressList.addAll(JsonHelper.parseCollection(array, ArrayList.class, GetUserAddressList.class));
//                    LogUtils.i("xxxxxx", "addressList的大小是：" + addressList.get(0).toString());
//                    myAdapter = new ListAdapter();
//                    lv_home_choAddress.setAdapter(myAdapter);
//                    myAdapter.notifyDataSetChanged();
                } catch (JSONException e) {

                }
            }
        });
    }
}
